package office.poi.excel.imp;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import office.poi.excel.bean.ExcelColumnBean;
import office.poi.excel.utils.ColumnUtils;
import office.poi.excel.utils.MsgUtils;
import office.poi.excel.utils.RowUtils;
import office.poi.excel.utils.ScanBeanUtil;


/**
 * Excel导入类
 * 
 * @author zhdy date 2013-1-5
 * @param <T>
 * 
 */
public class ImportExcel extends ReadExcel{
	private Class<?> clazz;
	public ImportExcel(Class<?> clazz) {
		this.clazz = clazz;
	}
	/**
	 * 根据提交的Excel 扫描对应的实体类注解 转成对应的List<?>泛型集合
	 * *注意：该方法只扫描简单的Excel，就是只会得到一个泛型List集合 *Excel的标题必须是同一行，没有行、列的合并样式
	 * 
	 * @param file
	 * @return
	 */
	@SuppressWarnings({ "rawtypes"})
	public Collection<?> getSimpleList(File file) {
		Collection list = new ArrayList();
		// 扫描实体类把ExcelColumn注解拆解
		List<ExcelColumnBean> ecbList = ScanBeanUtil.getWriteMethod(clazz);
		if (ecbList.size() > 0) {
			try {
				FileInputStream fis = new FileInputStream(file);
				HSSFWorkbook workbook = new HSSFWorkbook(fis);
				HSSFSheet sheet = workbook.getSheetAt(0);
				// 总行
				int LastRow = sheet.getLastRowNum();
				// 起始行的索引
				String name =  ecbList.get(0).getColumnName();
				int rowIndex = RowUtils.getRowIndex(sheet,name)[0];
				// 列索引集合
				int[] columnArr = new int[ecbList.size()];
				/**
				 * 遍历出列的索引
				 */
				for (int j = 0; j < ecbList.size(); j++) {
					// 列索引
					int columnIndex = ColumnUtils.getColumnIndex(sheet, ecbList.get(j).getColumnName())[0];
					if (columnIndex > 0) {
						columnArr[j] = columnIndex;
					}
				}
				list = getList(sheet, ecbList, LastRow, rowIndex, columnArr, clazz);
			} catch (Exception e) {
				e.printStackTrace();
				hasError = true;
				MsgUtils.addErrorMsg(errorMsg,"Excel读取发生错误！");
			}
		}
		return list;
	}
    /**
     * 获取多个list 
     * Excel 中存在 多组的ColumnName 则应该返回多个list集合
     * 多组的ColumnName 同属一组 即都为一个bean中的对象
     * @param file
     * @return
     */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<Integer,Collection<?>> getMapList(File file) {
		Collection list = new ArrayList();
		Map<Integer,Collection<?>> map = new LinkedHashMap();
		// 扫描实体类把ExcelColumn注解拆解
		List<ExcelColumnBean> ecbList = ScanBeanUtil.getWriteMethod(clazz);
		if (ecbList.size() > 0) {
			try {
				//读取Excel
				FileInputStream fis = new FileInputStream(file);
				HSSFWorkbook workbook = new HSSFWorkbook(fis);
				HSSFSheet sheet = workbook.getSheetAt(0);
				// 总行
				int LastRow = sheet.getLastRowNum();
				//**********标题行**************** 的索引集合
				int l = 0;
				if(ScanBeanUtil.REQUIRED_FLAG!=-1)l=ScanBeanUtil.REQUIRED_FLAG;
				int[] rowIndex = RowUtils.getRowIndex(sheet, ecbList.get(l).getColumnName());
				//**************************标题行 各列的索引******************************************************/
				int[][] rowsColumns = new int[rowIndex.length][ecbList.size()]; 
				
				if (rowsColumns.length <= 0) {
					hasError = true;
					MsgUtils.addErrorMsg(errorMsg,"Excel格式不正确，导致读取不到数据，请检查Excel格式是否与系统要求一致！");
				}
				
				for (int i = 0; i < rowIndex.length; i++) {
					for (int j = 0; j < ecbList.size(); j++) {
						//获取列数组
						rowsColumns[i][j] = ColumnUtils.getFirstColumnIndexByRowIndex(sheet, ecbList.get(j).getColumnName(),rowIndex[i]);
					}
				}
				for (int i = 0; i < rowsColumns.length; i++) {
					int[] columns = rowsColumns[i];
					int totalRow = (i+1) == rowIndex.length ? LastRow:rowIndex[i+1]-1;
					list = getList(sheet, ecbList, totalRow, rowIndex[i], columns, clazz);
					map.put(i, list);
					
				}  
			} catch (Exception e) {
				e.printStackTrace();
				hasError = true;
				MsgUtils.addErrorMsg(errorMsg,"Excel读取发生错误！");
			}
		}
		return map;
	}
    public LinkedHashMap<?,?> getLinkedHashMapNotSortColumn(final File file,final String[] columns, final String requiredColumn) throws Exception{
    	
    	if(columns.length>0)
		{
    		LinkedHashMap map = new LinkedHashMap<String,String>();	
    	    
    		int[] columnsIndex =new int[columns.length] ;
    		//读取Excel
			FileInputStream fis = new FileInputStream(file);
			HSSFWorkbook workbook = new HSSFWorkbook(fis);
			HSSFSheet sheet = workbook.getSheetAt(0);
    		
    		for (int i = 0; i < columns.length; i++) {				
  			  columnsIndex[i] = ColumnUtils.getColumnIndex(sheet, columns[i])[0];
  			}
		}
    	
    	
    	
    	return null;
    }


}
